package www.yingdi365.com.base;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;

import www.yingdi365.com.R;
import www.yingdi365.com.activity.DengLuActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppLoginResult;
import www.yingdi365.com.util.ACache;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class ZjbBaseFragment extends Fragment {
    //    private ProgressDialog mProgressDialog;
    private boolean isFinish = false;
    public boolean isLogin = false;
    public int changeControl = 2016;
    private AlertDialog mBeginDialog;
    private ProgressDialog mProgressDialog;
    public AppLoginResult appLoginResult;
    private AlertDialog reLoginDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁止横屏
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    public void init() {
        //添加当前界面到容器中
//        changeControl = Constant.changeControl - 1;
        initSP();
        initIntent();
        findID();
        initViews();
        setListeners();
    }

    protected abstract void initIntent();

    protected abstract void initSP();

    protected abstract void findID();

    protected abstract void initViews();

    protected abstract void setListeners();

    protected abstract void initData();

    private void initLogin() {
        ACache aCache = ACache.get(getActivity(), Constant.ACACHE.YING_DI);
        appLoginResult = (AppLoginResult) aCache.getAsObject(Constant.ACACHE.USER_INFO);
        if (appLoginResult != null) {
            isLogin = true;
        } else {
            isLogin = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initLogin();
        if (changeControl != Constant.changeControl) {
            initData();
            changeControl++;
        }
    }

    public void showLoadingDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        cancelLoadingDialog();
                        getActivity().finish();
                    }
                    return false;
                }
            });
        }
    }

    public void cancelLoadingDialog() {
//        if (mProgressDialog != null && mProgressDialog.isShowing() && !isFinish) {
//            mProgressDialog.dismiss();
//            mProgressDialog = null;
//        }
        if (mProgressDialog != null && mProgressDialog.isShowing() && !isFinish) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public void showReLoginDialog() {
        if (reLoginDialog == null) {
            reLoginDialog = new AlertDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setMessage("Token失效，请重新登录")
                    .setCancelable(false)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ACache aCache = ACache.get(getActivity(), Constant.ACACHE.YING_DI);
                            aCache.clear();
                            reLoginDialog.dismiss();
                            toLoginActivity();
                        }
                    })
                    .create();
            reLoginDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        ACache aCache = ACache.get(getActivity(), Constant.ACACHE.YING_DI);
                        aCache.clear();
                        reLoginDialog.dismiss();
                        toLoginActivity();
                    }
                    return false;
                }
            });
            reLoginDialog.show();
        } else {
            reLoginDialog.show();
        }
    }

    public void showReLoadDialog() {
        if (mBeginDialog == null) {
            final View dialog_begin = getLayoutInflater(getArguments()).inflate(R.layout.dialog_begin, null);
            mBeginDialog = new AlertDialog.Builder(getActivity()).setView(dialog_begin)
                    .create();
            mBeginDialog.setCancelable(false);
            mBeginDialog.show();
            dialog_begin.findViewById(R.id.buttonReLoad).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mBeginDialog.dismiss();
                    initData();
                }
            });
            mBeginDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        cancelLoadingDialog();
                        getActivity().finish();
                    }
                    return false;
                }
            });
        } else {
            mBeginDialog.show();
        }
    }

    public void cancelReLoadDialog() {
        if (mBeginDialog != null && mBeginDialog.isShowing() && !isFinish) {
            mBeginDialog.dismiss();
            mBeginDialog = null;
        }
    }

    /**
     * 单个按钮无操作
     *
     * @param msg
     */
    public void showTipDialog(String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(msg)
                .setPositiveButton("是", null)
                .create()
                .show();
    }

    public void toLoginActivity() {
        Intent intent = new Intent(getActivity(), DengLuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        // 杀掉进程
        Process.killProcess(Process.myPid());
        System.exit(0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isFinish = true;
    }


}
